/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package cn.tedu.luban.account.main.converter;


import cn.tedu.luban.account.protocol.VO.AccountVO;
import cn.tedu.luban.account.protocol.bo.AccountBO;
import cn.tedu.luban.account.protocol.dos.AccountDO;
import cn.tedu.luban.account.protocol.dto.AccountDTO;
import cn.tedu.luban.account.protocol.param.AccountCreateParam;
import cn.tedu.luban.account.protocol.param.AccountParam;
import cn.tedu.luban.account.protocol.param.AccountPayParam;
import cn.tedu.luban.account.protocol.query.AccountDBPagerQuery;
import cn.tedu.luban.account.protocol.query.AccountQuery;
import cn.tedu.luban.passport.protocol.jwt.UserPrinciple;
import cn.tedu.luban.passport.sdk.security.LubanSecurityContext;
import com.tedu.inn.protocol.ListRecordTotalBO;
import com.tedu.inn.protocol.pager.PagerResult;
import com.tedu.inn.protocol.pager.SimplePagerQuery;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;
@Component
public class AccountConverter {
    public AccountVO bo2vo(AccountBO bo) {
        AccountVO account = new AccountVO();
        if (bo == null) {
            return account;
        }
        BeanUtils.copyProperties(bo, account);
        return account;
    }
    public List<AccountVO> bos2vos(List<AccountBO> list) {
        if (CollectionUtils.isEmpty(list)) {
            return Collections.emptyList();
        }
        List<AccountVO> accountVOList = new ArrayList<>(list.size());
        for (AccountBO accountBo : list) {
            AccountVO accountVo = this.bo2vo(accountBo);
            accountVOList.add(accountVo);
        }
        return accountVOList;
    }
    public PagerResult<AccountVO> bo2PagerResultVO(ListRecordTotalBO<AccountBO> accountListTotalRecord,
        SimplePagerQuery accountQuery) {
        List<AccountVO> accountVOList = this.bos2vos(accountListTotalRecord.getList());
        PagerResult<AccountVO> pagerResult = new PagerResult<>(accountQuery);
        pagerResult.setObjects(accountVOList);
        pagerResult.setTotal(accountListTotalRecord.getTotal());
        return pagerResult;
    }
    public AccountDBPagerQuery query2dbQuery(AccountQuery accountQuery) {
        if (accountQuery == null) {
            return new AccountDBPagerQuery();
        }
        AccountDBPagerQuery accountDBPagerQuery = new AccountDBPagerQuery();
        BeanUtils.copyProperties(accountQuery, accountDBPagerQuery);
        return accountDBPagerQuery;
    }
    public AccountDO param2do(AccountCreateParam param) {
        AccountDO account = new AccountDO();
        if (param == null) {
            return account;
        }
        BeanUtils.copyProperties(param, account);
        UserPrinciple loginUser= LubanSecurityContext.getLoginToken();
        account.setGmtCreate(System.currentTimeMillis());
        account.setGmtModified(account.getGmtCreate());
        account.setCreateUserId(loginUser!=null?loginUser.getId():param.getUserId());
        account.setModifiedUserId(loginUser!=null?loginUser.getId():param.getUserId());
        account.setCreateUserName(loginUser!=null?loginUser.getUsername():"mock");
        account.setModifiedUserName(loginUser!=null?loginUser.getUsername():"mock");
        account.setStatus(1);
        account.setSettlingAmount(0L);
        account.setTotalAmount(0L);
        return account;
    }
    public AccountDO param2do(AccountParam param) {
        AccountDO account = new AccountDO();
        if (param == null) {
            return account;
        }
        BeanUtils.copyProperties(param, account);
        UserPrinciple loginUser= LubanSecurityContext.getLoginToken();
        account.setGmtCreate(System.currentTimeMillis());
        account.setGmtModified(account.getGmtCreate());
        account.setCreateUserId(loginUser!=null?loginUser.getId():param.getUserId());
        account.setModifiedUserId(loginUser!=null?loginUser.getId():param.getUserId());
        account.setCreateUserName(loginUser!=null?loginUser.getUsername():"mock");
        account.setModifiedUserName(loginUser!=null?loginUser.getUsername():"mock");
        return account;
    }
    public AccountDO param2do(AccountPayParam param) {
        AccountDO account = new AccountDO();
        if (param == null) {
            return account;
        }
        BeanUtils.copyProperties(param, account);
        account.setSettlingAmount(param.getTotalAmount());
        UserPrinciple loginUser= LubanSecurityContext.getLoginToken();
        account.setGmtCreate(System.currentTimeMillis());
        account.setGmtModified(account.getGmtCreate());
        account.setCreateUserId(loginUser!=null?loginUser.getId():999L);
        account.setModifiedUserId(loginUser!=null?loginUser.getId():999L);
        account.setCreateUserName(loginUser!=null?loginUser.getUsername():"system");
        account.setModifiedUserName(loginUser!=null?loginUser.getUsername():"system");
        return account;
    }
    public AccountBO do2bo(AccountDO account) {
        AccountBO accountBO = new AccountBO();
        if (account == null) {
            return accountBO;
        }
        BeanUtils.copyProperties(account, accountBO);
        return accountBO;
    }
    public List<AccountBO> dos2bos(List<AccountDO> list) {
        List<AccountBO> accountBos = new ArrayList<>(list.size());
        for (AccountDO account : list) {
            accountBos.add(this.do2bo(account));
        }
        return accountBos;
    }

    public AccountParam assembleParam(AccountCreateParam param) {
        AccountParam accountParam=new AccountParam();
        if (param == null) {
            return accountParam;
        }
        BeanUtils.copyProperties(param,accountParam);
        return accountParam;
    }

    public AccountDTO bo2dto(AccountBO account) {
        AccountDTO accountDTO=new AccountDTO();
        if (account == null) {
            return accountDTO;
        }
        BeanUtils.copyProperties(account,accountDTO);
        return accountDTO;
    }

    public AccountParam bo2param(AccountBO account) {
        AccountParam accountParam=new AccountParam();
        if (account == null) {
            return accountParam;
        }
        BeanUtils.copyProperties(account,accountParam);
        return accountParam;
    }
}